package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.tanhua.dubbo.server.pojo.TaoHua;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

@Service(version = "1.0.0")
public class TaoHuaApiImpl implements TaoHuaApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public String saveYY(TaoHua taoHua) {
        if (StrUtil.isEmpty(taoHua.getTaohuaurl())) {
            return null;
        }
        Long count = this.YyCount(taoHua.getUserId());
        if (count < 10) {
            TaoHua save = this.mongoTemplate.save(taoHua);
            return "今日已传" + (count + 1) + "个语音";
        }
        return "今日已传" + 10 + "个语音,请明日再试！";
    }

    @Override
    public Long YyCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        long count = this.mongoTemplate.count(query, TaoHua.class);
        if (count == 11) {
            return null;
        }
        return count;
    }

    @Override
    public TaoHua saveCy() {
//        List<TaoHua> all = this.mongoTemplate.findAll(TaoHua.class);
//        List<Long> userIds = CollUtil.getFieldValues(all, "userId", Long.class);
        Query query = Query.query(Criteria.where("userId").gt(1L));
        List<TaoHua> taoHuas = this.mongoTemplate.find(query, TaoHua.class);


        Random random = new Random();
        int i = random.nextInt(taoHuas.size());
        return taoHuas.get(i);
    }
}
